package com.ruoyi;


import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.Expression;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.mockito.Mockito;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Run a script under examples folder.
 *
 * @author dennis(killme2008@gmail.com)
 *  注入全局变量到脚本
 */
public class bVars {

    public static void main(final String[] args) throws Exception {
        Expression exp = AviatorEvaluator.getInstance().compileScript("examples/b.av",false);
        SysUser user = new SysUser();
        user.setUserName("linziheng");
        user.setUserId(1L);
        user.setPhonenumber("1");
        SysUser user1 = new SysUser();
        user1.setUserName("zhangsan");
        user1.setUserId(2L);
        user1.setPhonenumber("2");
        List<SysUser> list = new ArrayList<>();
        list.add(user1);
        list.add(user);
        // Mock a result set.
        ResultSet resultSet = Mockito.mock(ResultSet.class);
        Mockito.when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
        Mockito.when(resultSet.getString("username")).thenReturn("dennis").thenReturn("catty");
        Mockito.when(resultSet.getInt("age")).thenReturn(30).thenReturn(20);

        // Use it in aviator
        exp.execute(exp.newEnv("results", list));
    }

}
